package com.example.ross.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.ross.entity.User;
import com.example.ross.model.ResultFormat;
import com.example.ross.model.ResultFormatPaging;
import com.example.ross.service.UserService;
import com.example.ross.utils.ResultPagingUtil;
import com.example.ross.utils.ResultUtil;
import com.example.ross.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 前端控制器
 * @author RossZhang
 */
//@Controller
@RestController
@Api(tags = "用户信息管理")
@RequestMapping("/users")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private UserService userService;

    /**
     * 分页全查询用户信息
     *
     * @return
     */
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true, dataType = "Integer", defaultValue = "5")
    })

    /**
     * 全查询分类信息
     *
     * @return
     */
    @ApiOperation("查询用户信息接口")
    @GetMapping("/list")
    public ResultFormatPaging pgUsersList(User users, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        logger.info("-------------------进入用户查询controller层--------------------------");
        logger.info("users={}", users);
//        IPage<User> usersIPage = userService.selectUsersList(users, page, limit);
        IPage<User> usersIPage = userService.selectUserList(users,page,limit);
        List<User> usersList = usersIPage.getRecords();
        logger.info("usersList={}", usersList);
        return ResultPagingUtil.pagingSuccess(0, usersIPage.getTotal(), usersList);

    }


    /**
     * 添加用户信息
     *
     * @param users
     * @return
     */
    @ApiOperation("添加用户信息接口")
    @PostMapping("/save")
    @ResponseBody
    public ResultFormat savePgSorts( User users) {
        users.setRegisterTime(TimeUtil.dateTime());
        users.setUpdateTime(TimeUtil.dateTime());

//        ResultFormat insert = userService.saveUsers(users);
        ResultFormat insert = userService.saveUser(users);
        if (insert.getCode() == 200) {
            logger.info("添加成功");
            return ResultUtil.success();
        }
        if (insert.getCode() == 101) {
            logger.info("用户名已存在");
            return ResultUtil.error(insert.getCode(), insert.getMsg());
        }

        return ResultUtil.error(100, "添加失败");
    }


    /**
     * 修改用户信息
     *
     * @param users
     * @return
     */
    @ApiOperation("修改用户信息接口")
    @PutMapping("/update")
    @ResponseBody
    public ResultFormat updatePgSorts(User users) {

        users.getRegisterTime();
        users.setUpdateTime(TimeUtil.dateTime());

//        Integer insert = userService.updateUsers(users);
        Integer insert = userService.updateUser(users);
        if (insert == 101) {
            logger.info("用户名已存在");
            return ResultUtil.error(101, "用户名已经存在");
        }
        if (insert == 200) {
            logger.info("修改成功");
            return ResultUtil.success();
        }

        return ResultUtil.error(100, "修改失败");

    }


    /**
     * 修改用户状态
     *
     * @param user
     * @return
     */
    @ApiOperation("修改用户状态接口")
    @PutMapping("/delete")
    public ResultFormat deletePgSorts(User user) {

        logger.info("users=={}", user);

        boolean flag = userService.deleteUserId(user.getUserId());

        if (flag) {
            logger.info("修改成功");
            return ResultUtil.success();
        } else {
            logger.info("修改失败");
            return ResultUtil.error(100, "修改失败");
        }
    }


    /**
     * 批量删除用户信息
     * 状态为1 :已删除
     *
     * @param state
     * @param userId
     * @return
     */
    @PutMapping("/delBatchUsers")
    @ApiOperation(value = "批量删除用户信息")
    public ResultFormat delBatchLink(Integer state, @RequestParam(value = "userId[]") Integer[] userId) {
        logger.info("state={}", state);

        User users = new User();
        users.setState(1);
        boolean updateById = false;
        for (Integer integer : userId) {
            users.setUserId(integer);
            updateById = userService.deleteUserId(users.getUserId());
        }
        if (updateById) {
            return ResultUtil.success();
        }
        return ResultUtil.error(100, "修改失败");
    }
}
